<?php
namespace YunCangChu\Controller;
use Think\Controller;
class LoginController extends Controller {
    //登录首页
    public function index()
    {
        //echo 2;die;
        $this->display();
    }
    //发送验证码
    public function fyzm()
    {
        $phone = I('post.phone');
        if(S('denglu-'.$phone))
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>'您的频率过快,请稍后再试'));
        }
        $url = C('api_url')['手机发送验证码'];
        $data['FuncName']  = 'GET_VERIFICATION_CODE';
        $data['PhoneNo'] = $phone;
        $data['Purpose'] = 'L';
        $app_data          = baseMD5($data);
        $return_arr        = cUrl($url,$header=null, $app_data);
        if($return_arr['ReturnCode'] == 0)
        {
            S('denglu-'.$phone,1,60);
            $this->ajaxReturn(array('code'=>'0','msg'=>'发送成功'));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));
        }
    }
    //发送验证码
    public function fyzm1()
    {
        $phone = I('post.phone');
        if(S('xiugai-'.$phone))
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>'您的频率过快,请稍后再试'));
        }
        $url = C('api_url')['手机发送验证码'];
        $data['FuncName']  = 'GET_VERIFICATION_CODE';
        $data['PhoneNo'] = $phone;
        $data['Purpose'] = 'F';
        $app_data          = baseMD5($data);
        $return_arr        = cUrl($url,$header=null, $app_data);
        if($return_arr['ReturnCode'] == 0)
        {
            S('xiugai-'.$phone,1,60);
            $this->ajaxReturn(array('code'=>'0','msg'=>'发送成功'));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));
        }
    }
    //发送验证码
    public function fyzm2()
    {
        $phone = I('post.phone');
        if(S('zhuce-'.$phone))
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>'您的频率过快,请稍后再试'));
        }
        $url = C('api_url')['手机发送验证码'];
        $data['FuncName']  = 'GET_VERIFICATION_CODE';
        $data['PhoneNo'] = $phone;
        $data['Purpose'] = 'R';
        $app_data          = baseMD5($data);
        $return_arr        = cUrl($url,$header=null, $app_data);
        if($return_arr['ReturnCode'] == 0)
        {
            S('zhuce-'.$phone,1,60);
            $this->ajaxReturn(array('code'=>'0','msg'=>'发送成功'));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));
        }
    }
    //短信登录
    public function denglu()
    {
        if(!IS_AJAX)
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>'非法的请求'));
        }
        $phone = I('post.phone');
        $yzm = I('post.yzm');
        $url  = C('api_url')['短信登录'];
        $data['FuncName']  = 'USER_LOGIN_EXPRESS';
        $data['LoginID'] = $phone;
        $data['VCode'] = $yzm;
        $app_data          = baseMD5($data);
        $return_arr        = cUrl($url,$header=null, $app_data);
        if($return_arr['ReturnCode'] == 0)
        {
            //DES解密
            $url ="http://tool.chacuo.net/cryptdes";
            $data = array();
            $data['arg']  = 'm=ecb_pad=iso10126_p=88880000_o=1_s=utf-8_t=1';
            $data['type'] = 'des';
            $data['data'] = $return_arr['ReturnData'];
            $return_arr = cUrl($url,$header=null, $data);
            $userinfo = json_decode($return_arr['data'][0],true);
            Cookie('LoginID',$userinfo['uMobile']);
            $this->ajaxReturn(array('code'=>'0','msg'=>'登录成功'));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));
        }
    }
    //账号密码登录
    public function denglu1()
    {
        $phone3 = I('post.phone3');
        $password = I('post.password');
        $url = C('api_url')['密码登录'];
        $data['FuncName']  = 'USER_LOGIN';
        $data['LoginID']  = $phone3;
        $data['Passwd'] = $password;
        $app_data          = baseMD5($data);
        $return_arr        = cUrl($url,$header=null, $app_data);
        //print_r($data);;die;
        if($return_arr['ReturnCode'] == 0)
        {
            Cookie('LoginID',$phone3);
            $this->ajaxReturn(array('code'=>'0','msg'=>'登录成功'));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));
        }
    }
    //忘记密码
    public function Forget_the_password()
    {
        $this->display();
    }
    //找回密码
    public function zhaohui()
    {
        $yzm = I('post.yzm');
        $phone = I('post.phone');
        //$Passwd = I('post.password');
        $PasswdNew = I('post.queren');
        $url = C('api_url')['密码重置'];
        $data['FuncName']  = 'USER_RESET_PASSWORD';
        $data['LoginID']  = $phone;
        $data['Passwd'] = $PasswdNew;
        $data['VCode'] =$yzm;
        $app_data          = baseMD5($data);
        $return_arr        = cUrl($url,$header=null, $app_data);
        if($return_arr['ReturnCode'] == 0)
        {
            $this->ajaxReturn(array('code'=>'0','msg'=>'修改成功'));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));
        }
    }
    //注册
    public function zhuce()
    {
        if(IS_GET)
        {
            $this->display();
        }
        else
        {
            $phone = I('post.phone');
            $yzm = I('post.yzm');
            $pwd = I('post.password');
            $url = C('api_url')['手机号注册'];
            $data['FuncName']  = 'USER_REGIST';
            $data['LoginID']  = $phone;
            $data['Passwd'] = $pwd;
            $data['VCode'] = $yzm;
            $data['From'] = 'WeChat';
            $app_data          = baseMD5($data);
            $return_arr        = cUrl($url,$header=null, $app_data);
            if($return_arr['ReturnCode'] == 0)
            {
                Cookie('LoginID',$phone);
                $this->ajaxReturn(array('code'=>'0','msg'=>'注册成功'));
            }
            else
            {$this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));

            }
        }
    }
}